<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 16:46
 */

namespace app\admin\controller;


use think\Db;

class Member extends Base
{
    /**
     * 用户列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $where['user_status'] = ['neq', 3];
            $keyword = input('keyword');
            $start = input('start');
            $end = input('end');
            if(isset($keyword) && $keyword != ''){
                $where['user_id|user_username'] = ['like', '%' . $keyword . '%'];
            }
            
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['user_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['user_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['user_time'] = ['between',[$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('user_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function down(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 启用
     * @return \think\response\Json
     */
    public function up(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 详情
     * @return mixed
     */
    public function memberDetail(){
        $user_id = input('user_id');
        $info = Db::name('u_user')
            ->where('user_id', $user_id)
            ->find();
        $this->assign('info', $info);
        return $this->fetch('member/memberdetail');
    }
}